//
//  CustomView.m
//  自定义控件
//
//  Created by apple on 15/9/11.
//  Copyright (c) 2015年 戴维营. All rights reserved.
//

#import "CustomView.h"

@implementation CustomView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        CGSize size = frame.size;
        
        _aLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height * 0.8)];
        
        _aLabel.textAlignment = NSTextAlignmentCenter;
        
        _aLabel.font = [UIFont boldSystemFontOfSize:20];
        
        [self addSubview:_aLabel];
        
        _bLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, size.height * 0.8, size.width, size.height * 0.2)];
        
        _bLabel.font = [UIFont boldSystemFontOfSize:10];
        
        _bLabel.textAlignment = NSTextAlignmentCenter;
        
        [self addSubview:_bLabel];
    }
    
    return self;
}

//1、在对象被添加到其他视图时

//2、对象大小在发生变化的时候

//3、setneedslayout被调用时

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGSize size = self.frame.size;
    
    _aLabel.frame = CGRectMake(0, 0, size.width, size.height * 0.8);
    
    _bLabel.frame = CGRectMake(0, size.height * 0.8, size.width, size.height * 0.2);
    
//    _aLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height * 0.8)];
//    
//    _bLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, size.height * 0.8, size.width, size.height * 0.2)];
//    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//    if (_touchDown)
//    {
//        _touchDown(self,23);
//    }
//    
//    if (_changeColor)
//    {
//        UIColor *color = _changeColor();
//        _aLabel.backgroundColor = color;
//    }
    if (_delegate && [_delegate respondsToSelector:@selector(touchDown:index:)])
    {
        [_delegate touchDown:self index:0];
    }
    if (_delegate && [_delegate respondsToSelector:@selector(touchDown:index:)])
    {
        _aLabel.backgroundColor = [_delegate changeColor];
    }
    
}

@end
